import express from 'express'
import { deepClone, s2t, t2s } from '../../../util/common.js';
import { randSql, getResult, listSql, likeSql } from '../../../util/operateSql.js';
var router = express.Router();

const TABLE1 = 'resource.ciauthor'
const TABLE2 = 'resource.authors_song'
const TABLE3 = 'resource.authors_tang'

// 模糊获取作者
router.get('/poem_author',(req,res,next) => {
  let sql1 = '',sql2='',sql3=''
  if (req.query.name) {
    sql1 = likeSql(TABLE1,{name: req.query.name,pageSize:req.query.pageSize,orderBy:'value'})
    sql2 = likeSql(TABLE2,{name: s2t(req.query.name),pageSize:req.query.pageSize})
    sql3 = likeSql(TABLE3,{name: s2t(req.query.name),pageSize:req.query.pageSize})

    Promise.all([getResult(sql1),getResult(sql2),getResult(sql3)]).then((result) => {
      const reVal = result.flat(1).map(e => {
        return {
          id:e.id,
          author: t2s(e.name || e.author),
          paragraphs: t2s(e?.desc || e?.long_desc || e?.shrot_desc)?.replace(/[\"\[\]\\\,]/g,'').replace(/。/g,'\n')
        }
      })
      res.json(req.app.locals.success(reVal))
    }).catch((err) => {
      
      res.json(req.app.locals.error(err))
    });
  } else {
    res.json(req.app.locals.error('请输入正确参数'))
  }
})


export default router